﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.View.Controllers;
using Demo.CommonBase.View.Contracts;
using Demo.Services.Services.Impl.Managers;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Infos;

namespace Demo.Services.View.Controllers
{
    public sealed class ResourceController : AbstractDemoController
    {
        protected override void ConcreteExecute(IView view, string action)
        {
            try
            {
                switch (action)
                {
                    case "Services.GetOperations":
                        GetOperations(view);
                        break;
                    case "Services.GetResourceList":
                        GetResourceList(view);
                        break;
                    case "Services.DeleteResource":
                        DeleteResource(view);
                        break;
                    case "Services.GetResourceById":
                        GetResourceById(view);
                        break;
                    case "Services.SaveResource":
                        SaveResource(view);
                        break;
                    case "Services.GetAssignedTecnologies":
                        GetAsignedTecnologies(view);
                        break;
                    case "Services.GetUnAssignedTecnologies":
                        GetUnAsignedTecnologies(view);
                        break;                        
                    default: break;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void GetOperations(IView view)
        {
            OperationManager operationManager;
            OperationPagingInfo operationPaging;
            OperationFilter filter;
            try
            {
                Logger.Info();

                filter = (OperationFilter)view.GetValue("OperationFilter");
                operationManager = new OperationManager();
                operationPaging = operationManager.GetOperations(filter);
                view.SetValue("OperationPaging", operationPaging);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        private void GetResourceList(IView view)
        {
            ResourceManager resourceManager;
            ResourceResultInfo resourceList;
            ResourceFilter filter;
            PaginationInfo pagin; 
            try
            {
                Logger.Info();

                filter = (ResourceFilter)view.GetValue("ResourceFilter");
                pagin = (PaginationInfo)view.GetValue("ResourcePagin");
                resourceManager = new ResourceManager();
                resourceList = resourceManager.GetResources(pagin, filter);
                view.SetValue("ResourceList", resourceList);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void DeleteResource(IView view)
        {
            ResourceManager resourceManager;           
            int id = 0;            
            try
            {
                Logger.Info();

                id = (int)view.GetValue("Id");                
                resourceManager = new ResourceManager();
                resourceManager.DeleteResource(id);                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        private void GetResourceById(IView view)
        {
            ResourceManager resourceManager;                        
            ResourceInfo resourceInfo;
            int id = 0;
            try
            {
                Logger.Info();                
                id = (int)view.GetValue("Id");
                resourceManager = new ResourceManager();
                resourceInfo = resourceManager.GetResourceById(id);
                view.SetValue("ResourceInfo", resourceInfo);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        private void SaveResource(IView view)
        {
            ResourceManager resourceManager;            
            ResourceInfo resourceInfo;
            try
            {
                Logger.Info();

                resourceInfo = (ResourceInfo)view.GetValue("ResourceInfo");
                resourceManager = new ResourceManager();
                resourceManager.SaveResource(resourceInfo);                
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        private void GetAsignedTecnologies(IView view)
        {
            ResourceManager resourceManager;            
            List<TecnologyInfo> tecnologyList;
            int id = 0;
            try
            {
                Logger.Info();

                id = (int)view.GetValue("ResourceId");
                resourceManager = new ResourceManager();
                tecnologyList = resourceManager.getAssignedTecnologies(id);
                view.SetValue("AsignedTecnologyList", tecnologyList);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        private void GetUnAsignedTecnologies(IView view)
        {
            ResourceManager resourceManager;
            List<TecnologyInfo> tecnologyList;
            int id = 0;
            try
            {
                Logger.Info();

                id = (int)view.GetValue("ResourceId");
                resourceManager = new ResourceManager();
                tecnologyList = resourceManager.getUnAssignedTecnologies(id);
                view.SetValue("UnAsignedTecnologyList", tecnologyList);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        
    }
        
}
